package com.ihr360.rest.controller;

import com.alibaba.fastjson.JSONObject;
import com.ihr360.commons.lang.Enums;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("enum")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.ENUM_DATA, mainModuleName = ModuleCodeStore.Module.ENUM_DATA_NAME,
        isPublic = true)
public class RestEnumController {
    @RequestMapping("/{alias}.json")
    @FunctionDefinition(functionName = "获取枚举类型", functionCode = ModuleCodeStore.Function.VIEW)
    private JSONObject getValues(@PathVariable String alias) {
        JSONObject result = new JSONObject();
        @SuppressWarnings("rawtypes")
        Class<Enum> clazz = Enums.getEnumMetaClass(alias);
        if (null != clazz) {
            result.put("content", clazz.getEnumConstants());
            return result;
        }
        return result;
    }

    @PostMapping("/map.json")
    @FunctionDefinition(functionName = "获取枚举类型Map", functionCode = ModuleCodeStore.Function.VIEW)
    private Map<String, JSONObject> getValues(@RequestBody String[] aliases) {
        Map<String, JSONObject> result = new HashMap<>();
        for (int i = 0; i < aliases.length; i++) {
            result.put(aliases[i], getValues(aliases[i]));
        }
        return result;
    }

}